package com.thoughtworks.stafftimetable.main;

import com.thoughtworks.stafftimetable.domain.Consultant;
import com.thoughtworks.stafftimetable.parser.SourceDataParser;
import com.thoughtworks.stafftimetable.util.DateUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class StaffTimetableGenerator {

    private static final DateFormat headerDay = new SimpleDateFormat("EEE dd/MM");

    public static void main (String[] args) throws IOException, ParseException {
        InputStream inputStream = new FileInputStream(new File("c:/Australia.csv"));

        byte[] sourceData = IOUtils.toByteArray(inputStream);
        StaffTimetableGenerator generator = new StaffTimetableGenerator();

        String outputData = generator.convertSourceDataFormatToPersonCentricData(new String(sourceData), DateUtil.DATE_FORMATTER_FOR_SOURCE_DATA.parse("2008-01-21"));

        System.out.println(outputData);
    }

    public String convertSourceDataFormatToPersonCentricData(String sourceData, Date mondayStartDate) throws ParseException {
        SourceDataParser parser = new SourceDataParser();
        List<Consultant> consultants = parser.parse(sourceData);

        StringBuffer output = createOutputFileFromConsultantsMovements(mondayStartDate, consultants);

        return output.toString();
    }

    public StringBuffer createOutputFileFromConsultantsMovements(Date mondayStartDate, List<Consultant> consultants) {
        StringBuffer output = generateFirstHeaderLine(mondayStartDate);

        for (Consultant consultant : consultants) {
            output.append(createConsultantRow(consultant, mondayStartDate));
        }
        return output;
    }

    private StringBuffer createConsultantRow(Consultant consultant, Date mondayStartDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(mondayStartDate);

        StringBuffer consultantRow = new StringBuffer(consultant.getName());
        for (int i=0; i < 5; i++) {
            String assignmentForDay = consultant.getAssignmentForSpecifiedDay(cal.getTime());
            consultantRow.append("," + assignmentForDay);
            cal.add(Calendar.DAY_OF_WEEK, 1);
        }
        consultantRow.append("\n");
        return consultantRow;
    }

    private StringBuffer generateFirstHeaderLine(Date mondayStartDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(mondayStartDate);

        StringBuffer firstLine = new StringBuffer("Person");
        for (int i=0; i < 5; i++) {
            firstLine.append("," + headerDay.format(cal.getTime()));
            cal.add(Calendar.DAY_OF_WEEK, 1);
        }
        firstLine.append("\n");

        return firstLine;
    }
}
